<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-18 16:28:03
 * @Last Modified time: 2023-02-21 15:56:48
 */

namespace app\api\controller\store;	
use think\facade\App;
use app\common\services\store\OrderRefundService;
use frank\services\FileUploadService;
/**
 * Class OrderRefund
 */
class OrderRefund extends \app\api\controller\Base
{ 	
    /**
     * OrderRefundService constructor.
     * @param App $app
     */
    public function __construct(App $app,OrderRefundService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 售后单详情
     * @method(GET)
     */
    public function detail()
    {
        $id = $this->request->param('id');
        // 售后单详情
        $detail = $this->service->getOrderRefundDetail([
            'user_id' => $this->uid,
            'id' => $id
        ]);
        $this->success('获取成功',compact('detail'));
    }

    /**
     * 新增售后单记录
     * @method(POST)
     */
    public function create(){
        $data = $this->request->getMore([
            ['type', 1],
            ['order_goods_id', ''],
            ['type_desc', ''],
            ['content', ''],
            ['imgs', []],
        ]);

        $fileService = FileUploadService::instance();
        if(!empty($data['imgs'])){
            // 缓存图片移到指定目录
            $data['imgs'] = $fileService->moveCacheFiles($data['imgs'],'refund');
        }

        if ($this->service->createApply($this->uid,$data)) {
            $this->success('申请成功');
        }
        $this->error($this->service->getError() ?: '申请失败');
    }

    /**
     * 售后单列表
     * @method(GET)
     */
    public function list()
    {
        $dataType = $this->request->param('dataType');
        $filter['user_id'] = $this->uid;
        $dataType != 'all' && $filter['status'] = [1,3];
        $list = $this->service->getOrderRefundList($filter);
        return $this->success('获取成功',compact('list'));
    }

    /**
     * 取消售后单
     * @method(PUT)
     */
    public function cancel()
    {
        $id = $this->request->param('id');
        if ($this->service->cancel([
            'id'=>$id,
            'user_id'=>$this->uid
        ])) {
            $this->success('取消成功');
        }
        $this->error($this->service->getError() ?: '取消失败');
    }

    /**
     * 用户发货
     * @method(PUT)
     */
    public function delivery(){
        $data = $this->request->getMore([
            ['user_express_id', ''],
            ['user_express_no', ''],
            ['order_refund_id', ''],
        ]);
        if ($this->service->userDelivery($this->uid,
            $data)) {
            $this->success('提交成功');
        }
        $this->error($this->service->getError() ?: '提交失败');
    }

    /**
     * 用户确定收货
     * @method(PUT)
     */
    public function receipt(){
        $id = $this->request->param('id');
        if ($this->service->receipt(
            $id)) {
            $this->success('提交成功');
        }
        $this->error($this->service->getError() ?: '提交失败');
    }

}
